package cn.tedu.carhome.controller;

import cn.tedu.carhome.pojo.entity.User;
import cn.tedu.carhome.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired(required = false)
    UserMapper userMapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        System.out.println(">>>>>>>>>");
        System.out.println(user.getUsername());
        System.out.println(user.getDepartmentId());
        User user1 = userMapper.selectByUsername(user.getUsername());
        if (user1!=null){
            return 1;//失败
        }else {
            userMapper.insertUser(user);
            return 2;//成功
        }
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        User u = userMapper.selectByUsername(user.getUsername());
        if (u!=null){
            if (u.getPassword().equals(user.getPassword())){
                return 1;//登录成功
            }
            return 3;//密码错误
        }
        return 2;//用户名不存在
    }

}
